// pages/myInfo/myInfo.js
import { getProfile, editProfile, editPhoto } from '../../api/user'
const FormData = require('../../utils/formdata')
const dayjs = require("dayjs")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        profiles: {},
        showName: false,
        showGender: false,
        showBirthday: false,
        columns: ['男', '女'],
        currentDate: new Date().getTime(),
        minDate: new Date(1900,1,1).getTime(),
        maxDate: new Date().getTime(),
        formatter (type, value) {
            if (type === 'year') {
                return `${value}`
            }
            if (type === 'month') {
                return `${value}`
            }
            return value;
        },
    },
    // 获取用户个人资料
    async getProfile () {
        const res = await getProfile()
        this.setData({
            profiles: res.data,
            currentDate:new Date(res.data.birthday.replace(/[^0-9]/g, ',')).getTime()
        })
    },
    // 显示弹出层
    showNamePopup () {
        this.setData({ showName: true })
    },
    showGenderPopup () {
        this.setData({ showGender: true })
    },
    showBirthdayPopup () {
        this.setData({ showBirthday: true })
    },

    // 取消修改
    onCancel(){
        this.setData({
            showName: false,
            showBirthday: false,
            showGender: false
        })
    },

    // 修改头像
    async afterRead(e) {
        this.setData({
            'profiles.photo': e.detail.file.url
        })
        const formData = new FormData()
        formData.append('photo',this.data.profiles.photo)
        const data = formData.getData()
        console.log(data)
        const res = await editPhoto(data)
        console.log(res)
    },

    // 修改昵称
    async onChangeName() {
        console.log(this.selectComponent('.names'))
        await editProfile({name:this.selectComponent('.names').value})
        this.setData({
            showName: false
        })
        this.getProfile()
    },

    // 修改性别
    async onGenderConfirm(e) {
        // console.log(e.detail.index)
        await editProfile({gender:e.detail.index})
        this.setData({
            showGender: false
        })
        this.getProfile()
    }, 

    // 修改生日
    async onBirthdayConfirm(e) {
        const res = dayjs(e.detail).format("YYYY-MM-DD")
        await editProfile({birthday:res})
        this.setData({
            showBirthday: false
        })
        this.getProfile()

    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        this.getProfile()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage () {

    }
})